package com.portal.back.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.portal.back.service.ISysCustomService;
import com.portal.base.dao.BaseDao;
import com.portal.base.dao.IBaseDao;
import com.portal.domain.SysCustom;

/**
 * @Title:后台客户service
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2011-4-7
 * 
 */
public class SysCustomService implements ISysCustomService {
	private IBaseDao<SysCustom, Integer> dao;

	public SysCustomService() {
		dao = new BaseDao<SysCustom, Integer>(SysCustom.class);
	}

	public boolean savecustom(SysCustom custom) {
		if (custom == null)
			return false;
		String sql = "";
		try {
			if (custom.getId() == null) {
				sql = "insert into sys_custom(name,logoUrl,content,orderNum,type) values(?,?,?,?,?)";
				dao.insert(sql, new Object[] { custom.getName(),
						custom.getLogoUrl(), custom.getContent(),
						custom.getOrderNum(), custom.getType() });
			} else {
				sql = "update sys_custom  set name=?,logoUrl=?,content=?,orderNum=?,type=? where id=?";
				dao.update(sql,
						new Object[] { custom.getName(), custom.getLogoUrl(),
								custom.getContent(), custom.getOrderNum(),
								custom.getType(), custom.getId() });
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public List<SysCustom> getAllCustom() {
		String sql = "select * from sys_custom  ";
		List<SysCustom> list = dao.findList(sql);
		if (list != null && list.size() > 0) {
			List<SysCustom> customlist = new ArrayList<SysCustom>();
			for (int i = 0; i < list.size(); i++) {
				SysCustom custom = list.get(i);
				if ("1".equals(custom.getType()))
					custom.setTypeString("典型客户");
				else
					custom.setTypeString("经典客户");
				String content = custom.getContent();
				if (content != null && !"".equals(content)
						&& content.length() > 8)
					custom.setContent(content.substring(0, 8) + "...");
				customlist.add(custom);
			}
			return customlist;
		}
		return null;
	}

	public boolean deleteById(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "delete from sys_custom where  id=?";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from sys_custom where  id=" + delteid[0];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public SysCustom getById(String id) {
		String sql = "select * from sys_custom where  id=?";
		List<SysCustom> list = dao.findList(sql, new Object[] { id });
		if (list != null && list.size() > 0) {
			SysCustom custom = list.get(0);
			if ("1".equals(custom.getType()))
				custom.setTypeString("典型客户");
			else
				custom.setTypeString("经典客户");
			return custom;
		}
		return null;
	}

	public List<SysCustom> find(int start, int limit, String code) {
		String sql = "select * from sys_custom where type = '" + code + "'  order by orderNum";
		if (start >= 0 && limit > 0)
			sql = dao.createSqlQuery(sql).setFirstResult(start).setMaxSize(
					limit).bulidSql();
		return dao.findList(sql);
	}
}
